﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;


namespace FXnaMet
{

    /// <summary>
    /// 用于表示HUSWO格式
    /// </summary>
    [Serializable]
    public  class MetAllFile:MetSurfFile
    {

        List<AllRow> _data = new List<AllRow>();
        string _header = "";
        public override bool ReadFile(string fname)
        {
            this.Read(fname);
            return true;
        }


        protected void Read(string fname)
        {
            this._data.Clear();
            string[] split = new string[] { " " };
            using (StreamReader reader = new StreamReader(fname))
            {
                string line;
                //bool hasReadHeader = false;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] array = line.Split(split, StringSplitOptions.RemoveEmptyEntries);
                    if (array.Length == 20)
                    {
                        if (this._header == "")
                        {
                            this._header = line;
                            continue;
                        }
                    }
                    AllRow arow = new AllRow();
                    if (array.Length == 22)
                    {
                        arow.StationNumber = Convert.ToInt32(array[0]);
                        arow.ASOS = " ";
                        arow.Year = Convert.ToInt32(array[1]);
                        arow.Month = Convert.ToInt32(array[2]);
                        arow.Day = Convert.ToInt32(array[3]);
                        arow.Hour = Convert.ToInt32(array[4]);
                        arow.GRAD = Convert.ToInt32(array[5]);
                        arow.DRAD = Convert.ToInt32(array[6]);
                        arow.TotalCloud = Convert.ToInt32(array[7].Substring(0, 2));
                        arow.LowCloud = Convert.ToInt32(array[7].Substring(2, 2));
                        arow.DryTemperature = Convert.ToDouble(array[8]);
                        arow.Flag = " ";
                        arow.DewTemperature = Convert.ToDouble(array[9]);
                        arow.RelativeTemperature = Convert.ToInt32(array[10]);
                        arow.StationPressture = Convert.ToInt32(array[11]);
                        arow.WindDirection = Convert.ToInt32(array[12]);
                        arow.WindSpeed = Convert.ToDouble(array[13]);
                        arow.HVisibility = Convert.ToDouble(array[14]);
                        arow.CloudHeight = Convert.ToInt32(array[15]);
                        arow.NowSky = array[16].ToString();
                        arow.ASOSLayer1 = Convert.ToInt32(array[17]);
                        arow.ASOSLayer2 = Convert.ToInt32(array[18]);
                        arow.ASOSLayer3 = Convert.ToInt32(array[19]);
                        arow.Precipitation = Convert.ToInt32(array[20]);
                        arow.FlagPre = " ";
                        arow.SnowDepth = Convert.ToInt32(array[21]);
                    }
                    if (array.Length == 21)
                    {
                        arow.StationNumber = Convert.ToInt32(array[0]);
                        arow.ASOS = " ";
                        if (array[1].Length == 6)
                        {
                            arow.Year = Convert.ToInt32(array[1].Substring(0, 4));
                            arow.Month = Convert.ToInt32(array[1].Substring(4, 2));
                            arow.Day = Convert.ToInt32(array[2]);
                            arow.Hour = Convert.ToInt32(array[3]);

                        }
                        else if (array[2].Length == 3)
                        {
                            arow.Year = Convert.ToInt32(array[1]);
                            arow.Month = Convert.ToInt32(array[2].Substring(0, 1));
                            arow.Day = Convert.ToInt32(array[2].Substring(1, 2));
                            arow.Hour = Convert.ToInt32(array[3]);
                        }
                        else if (array[3].Length == 3)
                        {
                            arow.Year = Convert.ToInt32(array[1]);
                            arow.Month = Convert.ToInt32(array[2]);
                            arow.Day = Convert.ToInt32(array[3].Substring(0, 1));
                            arow.Hour = Convert.ToInt32(array[3].Substring(1, 2));
                        }
                        arow.GRAD = Convert.ToInt32(array[4]);
                        arow.DRAD = Convert.ToInt32(array[5]);
                        arow.TotalCloud = Convert.ToInt32(array[6].Substring(0, 2));
                        arow.LowCloud = Convert.ToInt32(array[6].Substring(2, 2));
                        arow.DryTemperature = Convert.ToDouble(array[7]);
                        arow.Flag = " ";
                        arow.DewTemperature = Convert.ToDouble(array[8]);
                        arow.RelativeTemperature = Convert.ToInt32(array[9]);
                        arow.StationPressture = Convert.ToInt32(array[10]);
                        arow.WindDirection = Convert.ToInt32(array[11]);
                        arow.WindSpeed = Convert.ToDouble(array[12]);
                        arow.HVisibility = Convert.ToDouble(array[13]);
                        arow.CloudHeight = Convert.ToInt32(array[14]);
                        arow.NowSky = array[15].ToString();
                        arow.ASOSLayer1 = Convert.ToInt32(array[16]);
                        arow.ASOSLayer2 = Convert.ToInt32(array[17]);
                        arow.ASOSLayer3 = Convert.ToInt32(array[18]);
                        arow.Precipitation = Convert.ToInt32(array[19]);
                        arow.FlagPre = " ";
                        arow.SnowDepth = Convert.ToInt32(array[20]);
                    }
                    if (array.Length == 20)
                    {
                        arow.StationNumber = Convert.ToInt32(array[0]);
                        arow.ASOS = " ";
                        if (array[1].Length == 6)
                        {
                            arow.Year = Convert.ToInt32(array[1].Substring(0, 4));
                            arow.Month = Convert.ToInt32(array[1].Substring(4, 2));
                            arow.Day = Convert.ToInt32(array[2].Substring(0, 1));
                            arow.Hour = Convert.ToInt32(array[2].Substring(1, 2));

                        }
                        else if (array[1].Length == 8)
                        {
                            arow.Year = Convert.ToInt32(array[1].Substring(0, 4));
                            arow.Month = Convert.ToInt32(array[1].Substring(4, 2));
                            arow.Day = Convert.ToInt32(array[1].Substring(6, 2));
                            arow.Hour = Convert.ToInt32(array[2]);
                        }
                        else if (array[2].Length == 5)
                        {
                            arow.Year = Convert.ToInt32(array[1]);
                            arow.Month = Convert.ToInt32(array[2].Substring(0, 1));
                            arow.Day = Convert.ToInt32(array[2].Substring(1, 2));
                            arow.Hour = Convert.ToInt32(array[2].Substring(3, 2));
                        }
                        arow.GRAD = Convert.ToInt32(array[3]);
                        arow.DRAD = Convert.ToInt32(array[4]);
                        arow.TotalCloud = Convert.ToInt32(array[5].Substring(0, 2));
                        arow.LowCloud = Convert.ToInt32(array[5].Substring(2, 2));
                        arow.DryTemperature = Convert.ToDouble(array[6]);
                        arow.Flag = " ";
                        arow.DewTemperature = Convert.ToDouble(array[7]);
                        arow.RelativeTemperature = Convert.ToInt32(array[8]);
                        arow.StationPressture = Convert.ToInt32(array[9]);
                        arow.WindDirection = Convert.ToInt32(array[10]);
                        arow.WindSpeed = Convert.ToDouble(array[11]);
                        arow.HVisibility = Convert.ToDouble(array[12]);
                        arow.CloudHeight = Convert.ToInt32(array[13]);
                        arow.NowSky = array[14].ToString();
                        arow.ASOSLayer1 = Convert.ToInt32(array[15]);
                        arow.ASOSLayer2 = Convert.ToInt32(array[16]);
                        arow.ASOSLayer3 = Convert.ToInt32(array[17]);
                        arow.Precipitation = Convert.ToInt32(array[18]);
                        arow.FlagPre = " ";
                        arow.SnowDepth = Convert.ToInt32(array[19]);
                    }
                    if (array.Length == 19)
                    {
                        if (array[1].Length == 10)
                        {
                            arow.StationNumber = Convert.ToInt32(array[0]);
                            arow.ASOS = " ";
                            arow.Year = Convert.ToInt32(array[1].Substring(0, 4));
                            arow.Month = Convert.ToInt32(array[1].Substring(4, 2));
                            arow.Day = Convert.ToInt32(array[1].Substring(6, 2));
                            arow.Hour = Convert.ToInt32(array[1].Substring(8, 2));
                            arow.GRAD = Convert.ToInt32(array[2]);
                            arow.DRAD = Convert.ToInt32(array[3]);
                            arow.TotalCloud = Convert.ToInt32(array[4].Substring(0, 2));
                            arow.LowCloud = Convert.ToInt32(array[4].Substring(2, 2));
                            arow.DryTemperature = Convert.ToDouble(array[5]);
                            arow.Flag = " ";
                            arow.DewTemperature = Convert.ToDouble(array[6]);
                            arow.RelativeTemperature = Convert.ToInt32(array[7]);
                            arow.StationPressture = Convert.ToInt32(array[8]);
                            arow.WindDirection = Convert.ToInt32(array[9]);
                            arow.WindSpeed = Convert.ToDouble(array[10]);
                            arow.HVisibility = Convert.ToDouble(array[11]);
                            arow.CloudHeight = Convert.ToInt32(array[12]);
                            arow.NowSky = array[13].ToString();
                            arow.ASOSLayer1 = Convert.ToInt32(array[14]);
                            arow.ASOSLayer2 = Convert.ToInt32(array[15]);
                            arow.ASOSLayer3 = Convert.ToInt32(array[16]);
                            arow.Precipitation = Convert.ToInt32(array[17]);
                            arow.FlagPre = " ";
                            arow.SnowDepth = Convert.ToInt32(array[18]);
                        }
                    }
                    this._data.Add(arow);
                }
            }
        }



          protected void Write(string fname)
        {
            FileStream fs = new FileStream(fname, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //if (this._header.Length > 0)
            //{
            if (this._header != null)
            {
                sw.WriteLine(this._header);
            }
                
                if (this._data.Count > 0)
                {
                    for (int i = 0; i < this._data.Count; i++)
                    { 
                        AllRow arow = this._data[i];
                        sw.Write(arow.StationNumber);
                        //sw.Write(arow.ASOS);
                        sw.Write("    ");
                        sw.Write(arow.Year);
                        sw.Write(" ");
                        sw.Write(arow.Month);
                        sw.Write(" ");
                        sw.Write(arow.Day);
                        sw.Write(" ");
                        sw.Write(arow.Hour);
                        sw.Write(" ");
                        sw.Write(arow.GRAD);
                       // sw.Write(" ");
                        sw.Write(arow.DRAD);
                        sw.Write("    ");
                        sw.Write(arow.TotalCloud);
                        sw.Write("    ");
                        sw.Write(arow.LowCloud);
                        sw.Write("    ");
                        sw.Write(Math.Round(arow.DryTemperature,1));
                        //sw.Write(arow.Flag+"   ");
                        sw.Write("    ");
                        sw.Write(Math.Round(arow.DewTemperature, 1));
                        sw.Write("    ");
                        sw.Write(arow.RelativeTemperature);
                        sw.Write("    ");
                        sw.Write(arow.StationPressture);
                        //sw.Write(arow.Flag+"   ");
                        sw.Write("    ");
                        sw.Write(arow.WindDirection);
                        sw.Write("    ");
                        sw.Write(Math.Round(arow.WindSpeed,1));
                        sw.Write("    ");
                        sw.Write(arow.HVisibility);
                        sw.Write("    ");
                        sw.Write(arow.CloudHeight);
                        sw.Write("    ");
                        sw.Write(arow.NowSky);
                        sw.Write("    ");
                        sw.Write(arow.ASOSLayer1);
                        sw.Write("    ");
                        sw.Write(arow.ASOSLayer2);
                        sw.Write("    ");
                        sw.Write(arow.ASOSLayer3);
                        sw.Write("    ");
                        sw.Write(arow.Precipitation);
                        //sw.Write(arow.FlagPre+"   ");
                        sw.Write("    ");
                        sw.WriteLine(arow.SnowDepth);
                    }
                }
           // }
        }
   

       



        public override bool WriteFile(string fname)
        {
            this.Write(fname);

            return true;



        }


    }


}
